package cn.jingzhuan.element.demo.view.epoxy.controller

import android.content.Context
import android.widget.Toast
import cn.jingzhuan.element.demo.view.activity.AlbumDetailActivity
import cn.jingzhuan.element.demo.view.epoxy.models.AudioRecyclerModel_
import cn.jingzhuan.element.demo.view.epoxy.models.BannerModel_
import cn.jingzhuan.element.demo.view.epoxy.models.TitleModel_
import cn.jingzhuan.element.demo.view.model.AudioItem
import cn.jingzhuan.element.demo.view.model.AudioAlbumItem
import cn.jingzhuan.element.demo.view.model.ItemDescription
import cn.jingzhuan.element.element.ItemAudioAlbumBindingModel_
import cn.jingzhuan.element.element.ItemBlockDividerWhiteBindingModel_
import com.airbnb.epoxy.AutoModel
import com.airbnb.epoxy.EpoxyController
import kotlinx.android.synthetic.main.view_module_title.view.*


class MusicAdapterController(var context: Context) : EpoxyController() {


    private var bannerList : MutableList<ItemDescription>  = mutableListOf()
    private var audioList : MutableList<AudioItem> = mutableListOf()
    private var audioAlbumList: MutableList<AudioAlbumItem> = mutableListOf()

    @AutoModel
    lateinit var bannerModel :BannerModel_
    @AutoModel
    lateinit var audioRecyclerModel : AudioRecyclerModel_


    override fun buildModels() {
        addBannerList(bannerList)
        addTitle("今日必听")
        addAudioRecycler() // 添加今日必听列表
        addBlockDivider()
        addTitle("专辑推荐")
        addAudioAlbums()
    }

    private fun addAudioAlbums() {
        if (!audioAlbumList.isEmpty()) {
            for (i in 0 .. (audioAlbumList.size-1)) {
                var audioAlbum = audioAlbumList[i]
                ItemAudioAlbumBindingModel_().id("AudioAlbum$i")
                        .audioAlbum(audioAlbum)
                        .clickListener({it->
                            AlbumDetailActivity.start(context,it.id)
//                            Toast.makeText(context,"正在点击${audioAlbum.title}",Toast.LENGTH_SHORT).show()
                        })
                        .addTo(this)
            }
        }
    }

    internal var id = 0

    private fun addAudioRecycler() {
        if (audioList != null && !audioList.isEmpty()) {
            audioRecyclerModel.audioList = audioList
            audioRecyclerModel.addTo(this)
        }
    }

    private fun addBlockDivider() {
        id += 1
        ItemBlockDividerWhiteBindingModel_().id(id).addTo(this)
    }

    //banner广告
    private fun addBannerList(mBannerList: MutableList<ItemDescription>) {
        if (mBannerList != null && !mBannerList.isEmpty()) {
            bannerModel.mBannerList = bannerList
            bannerModel.addTo(this)
        }
    }


    private fun addTitle(title:String) {
        id += 1
        var titleModel = TitleModel_()
        titleModel.title = title
        titleModel.id(id).addTo(this)
    }

    fun setBannerList(bannerList: MutableList<ItemDescription>) {
        if (this.bannerList == null) {
            this.bannerList = mutableListOf()
        }
        this.bannerList.clear()
        this.bannerList.addAll(bannerList)
        requestModelBuild()
    }

    fun setAudioList(audioList : MutableList<AudioItem>) {
        this.audioList.clear()
        this.audioList.addAll(audioList)
        requestModelBuild()
    }

    fun setAudioAlbumList(audioAlbumItemList : MutableList<AudioAlbumItem>) {
        this.audioAlbumList.clear()
        this.audioAlbumList.addAll(audioAlbumItemList)
        requestModelBuild()
    }

}